package com.xie.controller;

import com.xie.pojo.Course;
import com.xie.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/course")
public class CourseController {

    @Autowired
    @Qualifier("CourseServiceImpl")
    private CourseService courseService;

    @RequestMapping("/allCourse")
    public String allCourse(HttpSession session){
        List<Course> courses = courseService.queryAllCourse();
        session.setAttribute("courseList",courses);
        return "course";
    }

    @RequestMapping("/deleteCourse")
    public String deleteCourse(int cid, Model model){
        int i = courseService.deleteCourse(cid);
        if(i > 0){
            return "redirect:/course/allCourse";
        }else{
            model.addAttribute("msg","删除失败");
            return "redirect:/course/allCourse";
        }
    }

    @RequestMapping("/toAddCourse")
    public String toAddCourse(){
        return "addCourse";
    }
    @RequestMapping("/addCourse")
    public String addCourse(Course course,Model model){
        int i = courseService.addCourse(course);
        System.out.println(course);
        if (i > 0){
            return "redirect:/course/allCourse";
        }else{
            model.addAttribute("msg","添加失败");
            return "redirect:/course/toAddCourse";
        }
    }

    @RequestMapping("/toUpdateCourse")
    public String toUpdateCourse(int cid,Model model){
        Course course = courseService.queryCourse(cid);
        model.addAttribute("QCourse",course);
        return "updateCourse";
    }
    @RequestMapping("/updateCourse")
    public String updateCourse(Course course,Model model){
        int i = courseService.updateCourse(course);
        if (i > 0){
            return "redirect:/course/allCourse";
        }else{
            model.addAttribute("msg","修改失败");
            return "redirect:/course/allCourse";
        }
    }




}
